<template>
  <div class="dialog-footer">
    <el-form
      :model="saveDialog_form"
      status-icon
      :rules="rules"
      ref="saveDialog_form"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="文件名:" prop="name">
        <el-input
          type="text"
          v-model="saveDialog_form.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('saveDialog_form')"
          >确 定</el-button
        >
        <el-button @click="dialogVisible">取 消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { edgeWrite } from "@/api/siddhi";
import { base64EncodeUnicode } from "@/utils/utils";
import { constants } from "@/editor/js/tool-editor/constants";
export default {
  name: "",
  data() {
    return {
      saveDialog_form: { name: "" },
      config: "",
      rules: {
        name: [
          { required: true, message: "请输入文件名", trigger: "blur" },
          { min: 2, max: 30, message: "输入格式不正确", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9_-]{1,15}$/,
            message: "标识符在15个字符以内,只支持字母和数字！",
          },
        ],
      },
    };
  },
  methods: {
    dialogVisible() {
      this.$emit("closaddsite", false);
    },
    submitForm(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.playload();
          var param =
            "configName=" +
            base64EncodeUnicode(that.saveDialog_form.name + ".siddhi") +
            "&config=" +
            base64EncodeUnicode(that.config);
          edgeWrite(param)
            .then((res) => {
              this.$msg({ type: "success", message: "保存成功!" });
              this.$emit("closaddsite", false);
            })
            .catch((Error) => {
              console.log(Error);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    playload() {
      this.config = constants.INITIAL_SOURCE_INSTRUCTIONS; //siddhiFileEditor.getContent();
      let regexToExtractAppNameAnnotation =
        /@[Aa][Pp][Pp]:[Nn][Aa][Mm][Ee]\(['|"]/g;
      let regexExecuteResults = regexToExtractAppNameAnnotation.exec(
        this.config
      );
      let appNameAnnotation;
      if (regexExecuteResults == null) {
        appNameAnnotation = '@App:name("';
      } else {
        appNameAnnotation = regexExecuteResults[0];
      }
      let unicodeOfLastCharacter = appNameAnnotation.charCodeAt(
        appNameAnnotation.length - 1
      );
      let wrappingCodeTobeUsed = "";
      if (unicodeOfLastCharacter == 39) {
        wrappingCodeTobeUsed = "'";
      } else if (unicodeOfLastCharacter == 34) {
        wrappingCodeTobeUsed = '"';
      }
      let appNameToAdd =
        appNameAnnotation +
        this.saveDialog_form.name +
        wrappingCodeTobeUsed +
        ")";
      let appNameToRemove =
        appNameAnnotation + "SiddhiApp" + wrappingCodeTobeUsed + ")";

      // If @App:name is present
      if (regexExecuteResults != null) {
        this.config = this.config.replace(appNameToRemove, "");
      }
      this.config = appNameToAdd + "\n" + this.config;
    },
  },
};
</script>

<style scoped>
</style>
